using System.Collections;
using UnityEngine;

namespace abilities.triggers
{
    //时间触发器可以在发射时间的特定时间内触发，周期性的或一次性的，也可以限制重复次数。
    public class AbilityTriggerTimer : AbilityTrigger
    {

        ~AbilityTriggerTimer()
        {
            Debug.Log("~AbilityTriggerTimer %p" + this);
        }

        public override void Start()
        {
            Debug.Log("start %p" + this);
            float interval = prototype.GetInterval(context.effect.Level);
            float delay = prototype.delay;
            _coroutine = context.AbilityManager.StartCoroutine(iUpdate(interval,delay));
        }

        public override void Stop()
        {
            Debug.Log("stop %p" + this);
            context.AbilityManager.StopCoroutine(_coroutine);
        }

        private Coroutine _coroutine;
        IEnumerator iUpdate(float interval,float delay)
        {
            while (delay > 0)
            {
                delay -= Time.deltaTime;
                yield return 1;
            }

            float time = interval;
            while (interval > 0)
            {
                time -= Time.deltaTime;
                yield return 1;
                if (time <= 0)
                {
                    time = interval;
                    Update(interval);
                }
            }
        }
        public override void Update(float time)
        {
            OnPerform();
        }
    }
}